package com.hd.determine.config.rabbitmq;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title RabbitConfig.java
 * @Description rabbitmq 配置
 * @Package com.hd.determine.config.rabbitmq
 * @date 2021/1/6 3:53 下午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
@Configuration
public class RabbitConfig {

    @Value("${spring.rabbitmq.host}")
    public String host;

    @Value("${spring.rabbitmq.port}")
    public int port;

    @Value("${spring.rabbitmq.username}")
    public String userName;

    @Value("${spring.rabbitmq.password}")
    public String password;

    @Value("${spring.rabbitmq.virtual-host}")
    public String virtualHost;

    @Value("${spring.rabbitmq.prefetch-count}")
    public Integer prefetchCount;

    @Value("${spring.rabbitmq.concurrent-consumers}")
    public Integer concurrentConsumers;

    @Value("${spring.rabbitmq.max-concurrent-consumers}")
    public Integer maxConcurrentConsumers;

    @Bean(name = "connectionFactory")
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(userName);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        return connectionFactory;
    }

    @Bean("alarmContainerFactory")
    public SimpleRabbitListenerContainerFactory alarmContainerFactory() {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setPrefetchCount(prefetchCount);
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        factory.setConcurrentConsumers(concurrentConsumers);
        factory.setMaxConcurrentConsumers(maxConcurrentConsumers);
        factory.setReceiveTimeout(1000L);
        return factory;
    }

    @Bean("alarmGenContainerFactory")
    public SimpleRabbitListenerContainerFactory alarmGenContainerFactory() {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setPrefetchCount(10);
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        factory.setConcurrentConsumers(2);
        factory.setMaxConcurrentConsumers(4);
        factory.setReceiveTimeout(1000L);
        return factory;
    }


    @Bean
    public Queue initAlarmDetailQueue() {
        return new Queue(MqQueueConstant.ALARM_DETAIL_QUEUE, true);
    }


    @Bean
    public Queue initAlarmGenerateQueue() {
        return new Queue(MqQueueConstant.ALARM_GENERATE_QUEUE, true);
    }

    @Bean
    public Queue initAlarmUpdateQueue() {
        return new Queue(MqQueueConstant.ALARM_UPDATE_QUEUE, true);
    }

}
